(function(app){
	app
		.controller("content_21_inputCtrl",["$scope","$state","myFactory","$http","$stateParams","$q","$ionicHistory",function($scope,$state,myFactory,$http,$stateParams,$q,$ionicHistory){
			//参数定义、接收
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item02_1
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			var _productType = "";//产品类型
			//绑定参数到页面
			$scope.chdrnum01 = chdrnum01;
			$scope.readFlag = false;//阅读协议标志，默认为false
			$scope.ifShowReadFlag = false;//是否显示阅读协议，当订单含投连产品时候，就要显示，这个时候就只有同意才能下一步
			$scope.investDiv21Flag = false;
			$scope.universalDiv21Flag = false;
			$scope.ifNeedMoney = false;//设置万能金额是否必输选项
			//S2023062626494营运APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230803 start
	        $scope.butShow = true;
	        //S2023062626494营运APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230803 end
			var token=sessionStorage.getItem("token");
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}	
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}
			//页面同意阅读协议复选框部分
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
			}
/**********************************************初始化函数begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化查询变更项内容
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						itemData = response.data.data.modelMap;
						$scope.item21 = itemData.item21;
						$scope.accounts = itemData.accounts;
						if($scope.accounts!=undefined && $scope.accounts!=null && $scope.accounts!=""){
							for(var i=0;i<$scope.accounts.length;i++){
								$scope.accounts[i].funddesc=$scope.accounts[i].funddesc.replace("投资账户","");
							}
						}
						/*	
					  	 *  根据保单险种显示对应的产品输入区域
					  	 *
					  	 *	1. 当险种为投连产品时才显示[投连区域]
					  	 *	2. 当产品为万能产品时显示[万能区域]
					  	 *  3. 当产品为投连、万能产品时同时显示[投连区域]及[万能区域]
					  	 */
						//var _productType = "${item21.productType }" ;	//	产品类型
						_productType = $scope.item21.productType;//	产品类型
						
						//var _investDiv 	 = document.getElementById("investDiv21");	 //	投连区域
						//var investDiv_   = document.getElementById("investDiv22");   // 投连产品的提示
						//var _universalDiv= document.getElementById("universalDiv21");//	万能区域
						
//						_productType = "W";
//						$scope.item21.oldMoney = "1";
						//	存在投连产品时显示
						if( _productType.indexOf("U") > -1 ){
							//	取消隐藏
							//原：_investDiv.style.display = '';
							//原： investDiv_.style.display = '';
							$scope.investDiv21Flag = true;
							$scope.ifShowReadFlag = true;
						}
						//	存在万能产品时显示		
						if( _productType.indexOf("W") > -1 ){
							//原： _universalDiv.style.display ='';
							$scope.universalDiv21Flag = true;
						}
						
						//	设置万能金额是否必输选项
						//var _Money = document.getElementById("Money");
						//	为万能产品时必须输入万能金额
						//if( _productType == "W" ){
						//	_Money.required = "required" ;
						//}
						//	只为万能产品时必须输入万能金额
						if( _productType == "W" ){
							$scope.ifNeedMoney = true;
						}
						
						//S2023062626494APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230809 start
					    $scope.owAge = itemData.owAge;//投保人年龄
					    //S2023062626494APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230809 end

						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
/**********************************************初始化函数end****************************************************/				
			
			//客户须知
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindowOn21Input');
			}
			$scope.closeTip = function(){
				modal('close','tip_model','overwindowOn21Input');
			}
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn21Input','overwindowOn21Input','policyCloseXOn21Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn21Input = function(){
				modal('close','policyModalOn21Input','overwindowOn21Input','policyCloseXOn21Input');
			}
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			//下一步
			$scope.nextStep = function(){
				myFactory.loadingShow();//显示加载圈
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					myFactory.loadingHide();
					return;
				}
				//S2023062626494营运APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230803 start
				//if($scope.ifShowReadFlag == true){ //如果显示了申请须知，则需要检验是否勾选
					if( $scope.readFlag == false){ //如果没有勾选，返回
						myFactory.alert("","请阅读《账户提取风险提示》、《变更交易须知》！","info");
						myFactory.loadingHide();
						return false;
					}
				//}
				//S2023062626494营运APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230803 end
				
				var _accountRate = $("input[name='accountRateFor21Input']");	//	投资比例信息
				var _money 	  =  $("#moneyFor21Input").val().trim() ;			//	提取万能金额
				var _oldMoney = parseFloat($("#oldMoneyFor21Input").val().trim());//	原来万能金额
				if(isNaN(_oldMoney)){//检测得到的选项是不是数字，强制等于0
					_oldMoney = parseFloat("0");
        		}
				
				if($scope.ifNeedMoney == true ){//如果产品只为万能产品，那么一定要填写万能提取金额，对该金额是否为空进行校验
					if(_money ==""){
						myFactory.alert("","请输入提取金额！","info");
						myFactory.loadingHide();
						return false;
					}
					//var reg = /(^[0]{1}$)|(^[1-9]{1}[0-9]{0,9}$)|(^[0-9]{1}\.[0-9]{2}$)|(^[1-9]{2}[0-9]{0,9}\.[0-9]{2}$)/;
					var reg = /^[1-9]{1}\d*(\.\d{1,2})?$/;
					if(!reg.test(_money)){
						myFactory.alert("","请输入合法提取金额！","info");
						myFactory.loadingHide();
						return false;
					}
				}
				
				if( _money != "" ){//如果填入了提取金额，则直接转浮点型，否则置0
					_money = parseFloat(_money);
				} else {
					_money = 0 ;
				}	
				
				/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
	        	//当次卖出/转出投资账户超10个
	        	var accountOutCount = 0;
	        	/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
				var rateSum = 0 ; //	比例总值
		      	
		      	//	遍历所有投资账户信息，把不为空值的的投资比例信息进行总计
				for(var i = 0 ; i < _accountRate.length ; i ++ ){
					var rate = _accountRate[i].value ;	//	当前比例值
				 	if( "" != rate.trim() ){
				 		rateSum = rateSum + parseInt(rate);
				 		if(rate>100){
				 			myFactory.alert("","每一项账户提取比例值必须小于或等于100!","info");
							myFactory.loadingHide();
							return false;
				 		}
				 		/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
				 		accountOutCount++;
				 		/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
				 	}
				}
				/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 start*/
				if(accountOutCount>10){
					myFactory.alert("","当次卖出/转出投资账户不可超10个","info");
					myFactory.loadingHide();
					return false;
				}
				/*S2022032221697_中信保诚智尚人生年金保险C款新增“盛世嘉享、盛世优享”投资账户 add by huanghaokun 20220510 end*/
					
				if( _money > _oldMoney   ){
			 		myFactory.alert("","提取金额不能超过当前账户金额数值！","info");
			 		myFactory.loadingHide();
			   		return false;
			 	}	
				//	没有填写投连比例或是万能金额值时
			 	if( _money == 0 && 	rateSum == 0 ) {
			 		myFactory.alert("","请填写需要提取的投资账户信息或万能账户信息！","info");
			 		myFactory.loadingHide();
			    	return false;
			 	}
			  			
			 	/*客户说“部分提取”功能是每一项单独校验100%，不用总体合校验100%，因此这里注释，上面遍历也稍改动，改为检测到超过100%就给提示*/
				/*if( rateSum>100  && (_productType.indexOf("U") > -1)){ //如果存在投连产品，并且填写总的比例大于100，给出提示
					myFactory.alert("","提取比例值必须小于或等于100!","info");
					myFactory.loadingHide();
					return false;
				}*/	
				
				//S2023062626494APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230809 start
			    if($scope.owAge>=60 && $scope.universalDiv21Flag){
					$scope.extractionRiskOpen("2");
					myFactory.loadingHide();
					return false;
				}
			    //S2023062626494APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230809 end

			 	//校验全部通过，包装数据准备传送后台
				var a = $("input[name='accountTypeFor21Input']");//投资账户类型
				var b = $("input[name='accountUnitFor21Input']");//投资账户单位
		        var c = $("input[name='accountRateFor21Input']");//投资账户比例
		        var accountType = "";
		        var accountUnit = "";
		        var accountRate = "";
		        
		        for(var i=0;i<a.length;i++){//买入类型封装
		        	if(i!=0){
		        		accountType = accountType + ",";
		        	}
		        	accountType = accountType + $("#accountTypeFor21Input_"+i).val().trim();
		        }
		        for(var i=0;i<b.length;i++){
		        	if(i!=0){
		        		accountUnit = accountUnit + ",";
		        	}
		        	accountUnit = accountUnit + $("#accountUnitFor21Input_"+i).val().trim();
		        }
		        for(var i=0;i<c.length;i++){
		        	if(i!=0){
		        		accountRate = accountRate + ",";
		        	}
		        	accountRate = accountRate +  $("#accountRateFor21Input_"+i).val();
		        	accountRate = accountRate + " ";
		        }
			    
				var item={
			        "itemId":itemId,
			        "money":$("#moneyFor21Input").val().trim(),//万能投资金额       //注意这两个字段，如果是空的时候，传到后台不是0，必须是""，所以直接提取页面上就可以了
			        "oldMoney":$("#oldMoneyFor21Input").val().trim(),//原投资金额   //注意这两个字段，如果是空的时候，传到后台不是0，必须是""
			        "accountType":accountType,//投资账户类型
			        "accountUnit":accountUnit,//投资账户单位
			        "accountRate":accountRate,//投资账户比例
			    }
		        myFactory.loadingHide();
		        $ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy
					});
				})
				
			}
			
			//S2023062626494APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230809 start
			$scope.tonext = function(){
				$scope.extractionRiskClose();
				var a = $("input[name='accountTypeFor21Input']");//投资账户类型
				var b = $("input[name='accountUnitFor21Input']");//投资账户单位
		        var c = $("input[name='accountRateFor21Input']");//投资账户比例
		        var accountType = "";
		        var accountUnit = "";
		        var accountRate = "";
		        
		        for(var i=0;i<a.length;i++){//买入类型封装
		        	if(i!=0){
		        		accountType = accountType + ",";
		        	}
		        	accountType = accountType + $("#accountTypeFor21Input_"+i).val().trim();
		        }
		        for(var i=0;i<b.length;i++){
		        	if(i!=0){
		        		accountUnit = accountUnit + ",";
		        	}
		        	accountUnit = accountUnit + $("#accountUnitFor21Input_"+i).val().trim();
		        }
		        for(var i=0;i<c.length;i++){
		        	if(i!=0){
		        		accountRate = accountRate + ",";
		        	}
		        	accountRate = accountRate +  $("#accountRateFor21Input_"+i).val();
		        	accountRate = accountRate + " ";
		        }
				var item={
			        "itemId":itemId,
			        "money":$("#moneyFor21Input").val().trim(),//万能投资金额       //注意这两个字段，如果是空的时候，传到后台不是0，必须是""，所以直接提取页面上就可以了
			        "oldMoney":$("#oldMoneyFor21Input").val().trim(),//原投资金额   //注意这两个字段，如果是空的时候，传到后台不是0，必须是""
			        "accountType":accountType,//投资账户类型
			        "accountUnit":accountUnit,//投资账户单位
			        "accountRate":accountRate,//投资账户比例
			    }
		        myFactory.loadingHide();
		        $ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy
					});
				})
			}
			//S2023062626494APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230809 end
			
			//S2023062626494营运APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230803 start
	      	//账户提取风险提示
	        $scope.extractionRiskOpen = function(type) {
				if(type=='1'){
					$scope.butShow = true;
				}else{
					$scope.butShow = false;
				}
				modal('open', 'extractionRisk_model', 'overwindowOn21Input');
	        }
	        $scope.extractionRiskClose = function() {
				modal('close', 'extractionRisk_model', 'overwindowOn21Input');
	        }
	      	//S2023062626494营运APP养老欺骗风险防范&产品转换风险提示系统需求 add by huanghk 20230803 end	
			
			/**
			 * 只输入整数函授
			 */
			$scope.keyNumPress = function(){
				var keyCode = event.keyCode;
				if(keyCode>=48 && keyCode<=57){
					event.returnValue = true;
				}else{
					event.returnValue = false;
				}
			}
		}]);        
		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});
})(app)